package com.swt.mto.thithu.portlet;

import java.io.IOException;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.service.UserServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.swt.mto.thithu.model.Question;
import com.swt.mto.thithu.model.Quiz;
import com.swt.mto.thithu.model.QuizCategory;
import com.swt.mto.thithu.model.Result;
import com.swt.mto.thithu.service.QuestionLocalServiceUtil;
import com.swt.mto.thithu.service.QuizCategoryLocalServiceUtil;
import com.swt.mto.thithu.service.QuizLocalServiceUtil;
import com.swt.mto.thithu.service.ResultLocalServiceUtil;

/**
 * Portlet implementation class Exam
 */
public class ExamPortlet extends MVCPortlet { 
	
	public List<QuizCategory> getCategoryParent(){
		List<QuizCategory> quizCatelogies = null;
		quizCatelogies = QuizCategoryLocalServiceUtil.getCateByParentId(0);
		return quizCatelogies;
	}
	
	public void hi(){
		
	}
	
	public void calculateExam(ActionRequest actionRequest, ActionResponse actionResponse){
		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
		User user = themeDisplay.getUser();
		
//		ResultLocalServiceUtil.addResult(userId, quizId, numberSentenceCorrect, totalSentence, score)
	}
	
	@SuppressWarnings("null")
	public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
			throws PortletException, IOException {
		
		if(resourceRequest.getResourceID().equals("calculate-result")){
		
			long userId = (Long.valueOf(resourceRequest.getParameter("user")));
			long quizId = (Long.valueOf(resourceRequest.getParameter("quiz")));
			int countQuestion = (Integer.parseInt(resourceRequest.getParameter("countQuestion")));
			int correct = (Integer.parseInt(resourceRequest.getParameter("correct")));
			User user = null;
			try {
				user = UserServiceUtil.getUserById(userId);
				long resultId = CounterLocalServiceUtil.increment(Result.class.getName());
				Result result = ResultLocalServiceUtil.createResult(resultId);
				result.setUserId(userId);
				result.setUserName(user.getScreenName());
				result.setQuizId(quizId);
				result.setTotalSentence(countQuestion);
				result.setNumSentenceCorrect(correct);
				ResultLocalServiceUtil.addResult(result);

			} catch (PortalException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	
	public void getQuizByCategory(ActionRequest actionRequest, ActionResponse actionResponse)
			throws PortletException, IOException {
		PortletRequestDispatcher dispatcher = null;
		long cateId = Long.parseLong(actionRequest.getParameter("caId"));
		List<Quiz> quizs = QuizLocalServiceUtil.getQuizByCategoryId(cateId);
		actionRequest.setAttribute("listQuiz", quizs);
		dispatcher = getPortletContext().getRequestDispatcher("/html/exam/view.jsp");
		dispatcher.include(actionRequest, actionResponse);
	}
	
	public void getListResult(ActionRequest actionRequest, ActionResponse actionResponse)
			throws PortletException, IOException, SystemException {
		PortletRequestDispatcher dispatcher = null;
		List<Result> results = ResultLocalServiceUtil.getResults(0, ResultLocalServiceUtil.getResultsCount() - 1);
		actionRequest.setAttribute("listResult", results);
//		dispatcher = getPortletContext().getRequestDispatcher("/html/exam/userExam.jsp");
//		dispatcher.forward(actionRequest, actionResponse);

		actionResponse.setRenderParameter("jspPage", "/html/exam/userExam.jsp");
		sendRedirect(actionRequest, actionResponse);

	}
}
